package view
{
	import assets.view.MenuViewSymbol;
	
	import flash.display.Graphics;
	import flash.events.Event;
	
	import model.vo.ViewVO;
	
	import view.components.MenuButtonView;

	public class MenuView extends MenuViewSymbol
	{
		public static const NAME:String 				= "MenuView";
		public static const UPDATE:String				= NAME + "Update";
		public static const CLICKED:String				= NAME + "Clicked";
		public static const CHANGED:String				= NAME + "Changed";
		
		public static const SHOW:String					= NAME + "Show";
		
		private var menuData:Array;
		private var currentData:Object;
		
		private var viewData:ViewVO;
		
		private var currentBtn:MenuButtonView;
		private var numButtons:int;
		
		private var menuWidth:Number;
		private var menuHeight:Number;
		
		private var btnsArr:Array;
		
		public function MenuView()
		{
//			this.alpha = 0;
		}
		
		public function init( data:Array, viewVO:ViewVO ):void
		{
			menuData = data;
			viewData = viewVO;
			
			numButtons = menuData.length;
			
			setMenuViewProperties();
			buildMenuView();
		}
		
		public function show():void
		{
			FX.shuffleFadeIn( btnsArr );
		}
		
		private function setMenuViewProperties():void
		{
			menuWidth = Math.round( viewData.MENU_WIDTH );
			menuHeight = Math.round( viewData.MENU_HEIGHT );
		}
		
		private function buildMenuView():void
		{
			btnsArr = new Array();
			
			var btn:MenuButtonView;
			
			var i:uint;
			for( i = 0; i < numButtons; i++ )
			{
				btn = new MenuButtonView();
				btn.addEventListener( MenuButtonView.CLICKED, btnClickedHandler );
				btn.init( menuData[i], viewData, i );
				
				btn.x = btnsContainer.width;
				
				btnsContainer.addChild( btn );
				btnsArr.push( btn );
				
				if( i == 0 ) currentBtn = btn;
			}
			
			var g:Graphics = frameContainer.graphics;
			g.clear();
			g.lineStyle( 0.25, viewData.frameColor);
			g.drawRect( 0, 0, menuWidth, menuHeight );
			g.endFill();
			
			currentBtn.select();
			currentData = currentBtn.data;
//			dispatchEvent( new Event( CLICKED ) );
		}
		
		private function btnClickedHandler( e:Event ):void
		{
			currentBtn.unselect();
			
			currentBtn = e.target as MenuButtonView;
			
			currentData = currentBtn.data;
			
			dispatchEvent( new Event( CLICKED ) );
		}
		
		public function get data():Object
		{
			return currentData;
		}
		
		public function get index():int
		{
			return currentBtn.index;
		}
	}
}